<?php
/**
 * White Page Web Crawling
 */

use Fox\Common\Localization\SubRegion;
use Fox\Common\Localization\City;
use Fox\Common\Localization\City\Street;
use Fox\Html\CheckBox;
use Fox\Html\Input;
use Fox\Html\Screen;

set_time_limit(Xview::TIMEOUT_VERY_LONG);

$screen = new Screen("extract","extract");
$block = new Block("b","Extract Source Files");
$block->add(new TextArea("postal_code",true));
$block->add(new CheckBox("commit"));
$block->add(new Input("dbcnt",true,1));
$screen->add($block);

if($screen->posted())
{
	//$grid = new Fox\Grid("gResult");
	//$grid->setFieldCat(array(array("msgtp","type"),"name","ilink",array("ilink","olink"),"postal_code","nbstreets","msgtx"));
	$inputs = $screen->get();
	if(!Xview::instance()->errors())
	{
		$express = true;
		// Load Sub Regions Labels
		$finder = new SubRegion();
		$subregions = $finder->find(null,array("code" => false),null,null);
		foreach($subregions as $subregion)
		{
			$subRegionLabels[$subregion->code] = $subregion->regsub_name;
		}
		
		if(!empty($inputs['postal_code']))
		{
			$sql = null;
			$zipcodes = preg_split("/,/", $inputs['postal_code']);
			foreach($zipcodes as $zipcode)
			{
				if(empty($sql))
					$sql = " postal_code LIKE '$zipcode%' ";
				else
					$sql .= "OR postal_code LIKE '$zipcode%' ";
			}
			$fragments[] = new Fox\Dao\SqlFragment($sql);
		}
		$fragments[] = new Fox\Dao\SqlFragment("disable = 0 AND streets_status = 0");
		$fragment = Fox\Dao\SqlFragment::combine($fragments);
		$finder = new City();
		$cities = $finder->find($fragment,array("keyname" => false),null,$inputs["dbcnt"]);
		// Find City
		foreach($cities as $city)
		{
			$stNb = 0;
			// Localization
			$subRegCode = substr($city->postal_code,0,2);
			$subRegionLabel = strtoupper($subRegionLabels[$subRegCode]);
			$localite = $city->fullname." (".$city->postal_code." - ".$subRegionLabel.")";
			$localite = preg_replace("/ /","%20",strtoupper($localite));
			$departement = $subRegionLabel."%20(".$subRegCode.")";
			// Find Corresponding Streets
			foreach($city->streets() as $street)
			{		
				/* @var $street Street */
				$name = $street->name();
				$ficnm = _TEMP_.$city->cityid."-".$street->streetid.".html";
				if(is_file($ficnm))
				{
					$stNb++;
					continue;
				}
				$adress = $name." - ".$city->fullname." (".$city->postal_code.")";
				// Extract & Save web page in the local server
				$data = "btnValidSearch=true&j%5Fidt673=true&";
				if($express)
				{
					$data .= "ou=".preg_replace("/ /","%20",$adress);
					$data .= "typeFormulaire=formExpress";
					$targetPage = post_request('http://www.pagesjaunes.fr/pagesblanches/RecherchePagesBlanchesExpress.do?portail=PJ',$data);
			
				}
				else
				{
					$data .= "&adresse=".preg_replace("/ /","%20",$name);
					$data .= "&localite=".$localite;
					$data .= "&departement=".$departement;
					$targetPage = post_request('http://www.pagesjaunes.fr/pagesblanches/RecherchePagesBlanchesClassique.do?portail=PJ',$data);
			
				}
				var_dump($data);
				
				//$targetPage = post_request('http://www.pagesjaunes.fr/pagesblanches/RecherchePagesBlanchesExpress.do?portail=PJ',$data);
				if(!empty($targetPage["content"]))
				{
					
					if ($fp = fopen($ficnm,"w"))
					{
						fwrite($fp,$targetPage["content"]);
						fclose($fp);
					}
				}
				break;
				$stNb++;
				// Find Street Inhabitants
				
				// Save Inhabitants on the serveur
				
				// Flag Street 
			}
			if($stNb > 0)
			{
				$city->setFields(array("streets_status" => 1));
				try {
				//	$city->update();
					push_msg("S","City : $city->fullname has been loaded.");
				}
				catch(Fox\Object\Exception\Save $e)
				{
					push_msg("E",$e->getMessage());
				}
			}
			else
			{
				push_msg("I","City : $city->fullname has already been loaded.");
			}
		}
	}
}
echo $screen->display();